package com.itheima.service.mongo.impl;/*
 * @author  xs
 * @date 2022/1/16 1:38
 * @Version  1.0
 * */

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.UserLocation;
import com.itheima.domain.vo.UserPointVo;
import com.itheima.service.mongo.UserLocaleService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

@DubboService
public class UserLocaleServiceImpl implements UserLocaleService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public UserPointVo findLocaleById(Long userId) {
        Query query = new Query(
                Criteria.where("userId").is(userId)
        );
//        根据用户id查询地理位置
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        //判断是否为空
        if (userLocation==null){
            UserLocation userLocation1 = new UserLocation();
            userLocation1.setLocation(new GeoJsonPoint(116.635672,40.169419));
            userLocation1.setAddress("北京顺义黑马");
            mongoTemplate.save(userLocation1);
        }
            GeoJsonPoint location = userLocation.getLocation();
             userLocation.getAddress();//描述
//          封装Vo对象
             UserPointVo userPointVo = new UserPointVo();
             userPointVo.setLongitude(location.getX());//经度
             userPointVo.setLatitude(location.getY());//纬地
             userPointVo.setAddress(userLocation.getAddress());
        return userPointVo;
    }
}
